# demo01_dict.py
dict02 = {"age": 20, "status": True}
# 2. 更新或添加字典
dict02["age"] = 25  # key存在是更新操作
dict02["name"] = "nfx"  # key不存在，是添加操作
print(dict02)  # {'age': 25, 'status': True, 'name': 'nfx'}
# 字典.update(字典): 批量更新或添加
dict02.update({"age": 18, "email": "nfx@qq.com"})
print(dict02)  # {'age': 18, 'status': True, 'name': 'nfx', 'email': 'nfx@qq.com'}
# 字典.setdefault(key, default):key在原字典不存在用默认值覆盖,key存在则不修改
info = {"name": "zs", "score": 80}
info.setdefault("class", "未填写")
print("info:", info)  # {'name': 'zs', 'score': 80, 'class': '未填写'}
info.setdefault("name", "hahaha")
print("info:", info)  # {'name': 'zs', 'score': 80, 'class': '未填写'}
# 3. 删除
dict03 = {"name": "zs", "age": 18, "score": 100}
# 字典.pop(key): 通过key删除value，将删除的value进行返回
result = dict03.pop("name")
print(dict03, result)  # {'age': 18, 'score': 100} zs
# del 数据
del dict03["score"]
print(dict03)  # {'age': 18}
# 字典.clear(): 清空字典的数据
dict03.clear()
print(dict03)  # {}





print("=========================================================")
dict01 = {"name": "zhangsan", "age": 18}  # 创建字典
# 查询字典
# 1. 字典[key]
print(dict01["name"], dict01["age"])  # zhangsan 18
# print(dict01["email"])  报错，key不存在
# 2. 字典.get(key, 默认值)
print(dict01.get("name"), dict01.get("email"))  # zhangsan None
print(dict01.get("email", "暂未填写"))  # 暂未填写,使用默认值取代None
# 3. 获取所有的key
print(dict01.keys())  # dict_keys(['name', 'age'])
for item in dict01.keys():
    print("key:", item)
# 4. 获取所有的value
print(dict01.values())  # dict_values(['zhangsan', 18])
for item in dict01.values():
    print("value:", item)
# 5. 获取所有的 key-value
print(dict01.items())  # dict_items([('name', 'zhangsan'), ('age', 18)])
for item in dict01.items():
    print("item:", item)




# 休 ..... 123
# 王 ..... 345
# 后 ..... 123
# dict01 = {
#     # 键(key): 值(value)
#     "xiu": 123,    name:  ,  age:   , address:
#     "wang": 345,
#     "hou": 123
# }

